import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

//解决vue路由重复导航错误
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 主页面
const layout1 = () => import("@/components/layout1");
//实例
const router = new VueRouter({
  mode: "history",
  routes: [
    // 404
    {
      path: "*", //如果是无效路由的话
      component: () => import("@/view/404/404"),
      meta: { NotFound: true, title: "404" },
    },
    {
      // 默认显示home
      path: "/",
      redirect: "/home",
      component: layout1,
      children: [
        {
          path: "home",
          name: "home",
          component: () => import("@/view/home"),
          meta: { title: "初始化--介绍页面" },
        },
      ],
    },

    {
      // 最外的一层
      path: "/teacher",
      name: "teacher",
      component: layout1,
      redirect: "/teacher/teacherList",
      meta: { title: "教师管理" },
      children: [
        {
          path: "teacherList",
          name: "teacherList",
          component: () => import("@/view/teacher/teacherList"),
          meta: { title: "教师列表" },
        },
        {
          path: ":tno",
          name: "_tNo",
          component: () => import("@/view/teacher/_tNo"),
          meta: { title: "课程安排" },
          props({ params: { tno } }) {
            //连续结构
            return { tno };
          },
        },
      ],
    },

    {
      // 最外的一层
      path: "/student",
      name: "student",
      component: layout1,
      redirect: "/student/studentList",
      meta: { title: "学生管理" },
      children: [
        {
          path: "studentList",
          name: "studentList",
          component: () => import("@/view/student/studentList"),
          meta: { title: "学生列表" },
        },
        {
          path: ":sno",
          name: "_sNo",
          component: () => import("@/view/student/_sNo"),
          meta: { title: "课程详情" },
          props({ params: { sno } }) {
            //连续结构
            return { sno };
          },
        },
      ],
    },

    {
      // 最外的一层
      path: "/course",
      name: "course",
      component: layout1,
      redirect: "/course/courseList",
      meta: { title: "课程管理" },
      children: [
        {
          path: "courseList",
          name: "courseList",
          component: () => import("@/view/course/courseList"),
          meta: { title: "课程列表" },
        },
      ],
    },

    {
      // 最外的一层
      path: "/grade",
      name: "grade",
      component: layout1,
      redirect: "/grade/gradeList",
      meta: { title: "成绩管理" },
      children: [
        {
          path: "gradeList",
          name: "gradeList",
          component: () => import("@/view/grade/gradeList"),
          meta: { title: "成绩列表" },
        },
      ],
    },
  ],
});

// // 守卫;
// router.beforeEach((to, from, next) => {
//   console.log(to);
//   console.log(from);
//   next();
// });

router.afterEach((to, from) => {
  if (to.meta.title) document.title = to.meta.title;
});

//暴露
export default router;
